﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Diagnostics;
using System.IO;
using CStock;
using CUtil;
using CIndicators;


namespace CTrading
{
    public partial class DialogChartLine : Form
    {
         private IDataItem dataItem;
         private IIndicator indicator;
         private IDataItem dataItemNew;


        public DialogChartLine(IDataItem myDataItem)
        {
            dataItem = myDataItem;
            InitializeComponent();
            InitializeCustom();
        }

         private bool InitializeCustom()
         {
             this.indicator = new Indicator();
             FillCombo_Indicators();
             return true;

         }
         private void FillCombo_Indicators()
         {
             this.comboBoxIndicator.Items.Clear();
             try
             {
                 for (int i = 0; i < CIndicators.IndEvaluator.IndicatorsAvailable.Count; i++)
                 {
                     IIndicator ind = CIndicators.IndEvaluator.IndicatorsAvailable[i] as IIndicator;
                     this.comboBoxIndicator.Items.Add(ind.NameBase);
                 }
                
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.Assert(false, "Error in updating combo box " + ex.Message);
             }

         }
         private void FillCombo_DataItems()
         {
             this.comboBoxIndicator.Items.Clear();
             try
             {
                 IList<string> allShares = TradeEconomy.GetAllShares();
                 allShares = CUtil.CollectionUtils.SortListOfStrings(allShares);
                 for (int i = 0; i < allShares.Count; i ++ )
                 {
                     this.comboBoxIndicator.Items.Add(allShares[i]);

                 }

             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.Assert(false, "Error in updating combo box " + ex.Message);
             }

         }
         public IIndicator Indicator
         {
             get
             {
                 return this.indicator;
             }
         }
         public IDataItem DataItemNew
         {
             get
             {
                 return this.dataItemNew;
             }
         }
     

        private void comboBoxIndicator_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioButtonShare.Checked)
            {
                this.dataItemNew.ID = comboBoxIndicator.Text;
            }
            else
            {
                this.indicator.NameBase = comboBoxIndicator.Text;
            }
            
        }

        private void radioButtonShare_CheckedChanged(object sender, EventArgs e)
        {
            FillCombo_DataItems();
            this.indicator = null;
            this.dataItemNew = new DataItem();
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            FillCombo_Indicators();
            this.indicator = new Indicator();
            this.dataItemNew = null;

        }
        private int GetInterval()
        {
            string strInterval = textBoxInterval.Text;
            int interval = 2;
            if (!String.IsNullOrEmpty(strInterval))
            {
                try
                {
                    interval = Convert.ToInt32(strInterval);
                }
                catch
                {
                }
            }
            return interval;

        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            int interval = GetInterval();

            if (radioButtonShare.Checked)
            {
                this.dataItemNew.ID = comboBoxIndicator.Text;
            }
            else
            {
                this.indicator.NameBase = comboBoxIndicator.Text;
                this.indicator.Interval = interval;
            }

        }
    }
}
